package {{onceData.templateInfo.otherInfo.servicePackage}};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import {{onceData.templateInfo.otherInfo.mybatisPackage}}.mapper.{{capitalizeCamelTableName}}Mapper;
import {{onceData.templateInfo.otherInfo.mybatisPackage}}.model.{{capitalizeCamelTableName}};

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * {{tableComment}}服务层代码
 * @author 字母哥
 */
@Service
public class {{capitalizeCamelTableName}}Service {

  @Resource
  private {{capitalizeCamelTableName}}Mapper {{camelTableName}}Mapper;

  /**
   * 查询所有
   */
  public List<{{capitalizeCamelTableName}}> all(){
    return  {{camelTableName}}Mapper.selectList(null);
  }

  /**
   * 根据参数查询
{% for field in queryParamFields %}
   * @param {{field.columnName}} {{field.columnComment}}
{% endfor %}
   */
  public List<{{capitalizeCamelTableName}}> query(
{% for field in queryParamFields %}
{% if loop.last%}
        {{field.columnJavaType | split('.') | last}} {{field.columnName}} ) {
{% elif not loop.last %}
        {{field.columnJavaType | split('.') | last}} {{field.columnName}},
{% endif %}
{% endfor %}
    LambdaQueryWrapper<{{capitalizeCamelTableName}}> lambdaQ = Wrappers.lambdaQuery();
    lambdaQ
{% for field in queryParamFields %}
{% if loop.last%}
        .eq(StringUtils.isNotEmpty({{field.columnName}}),{{capitalizeCamelTableName}}::get{{field.columnNameUpperCase}},{{field.columnName}});
{% elif not loop.last %}
        .eq(StringUtils.isNotEmpty({{field.columnName}}),{{capitalizeCamelTableName}}::get{{field.columnNameUpperCase}},{{field.columnName}})
{% endif %}
{% endfor %}

    return {{camelTableName}}Mapper.selectList(lambdaQ);
  }

  /**
   * 根据id更新数据字典项
   * @param {{camelTableName}} 更新实体(包含id)
   */
  public void update({{capitalizeCamelTableName}} {{camelTableName}}){
    Assert.isTrue({{camelTableName}}.getId() != null,
      "更新数据必须指定数据更新条件（主键）");

    {{camelTableName}}Mapper.updateById({{camelTableName}});
  }

  /**
   * 新增数据字典项
   * @param {{camelTableName}} 新增实体
   */
  public void add({{capitalizeCamelTableName}} {{camelTableName}}){
    {{camelTableName}}.setCreateTime(new Date());
    {{camelTableName}}Mapper.insert({{camelTableName}});
  }

  /**
   * 根据id删除数据字典项
   * @param id  删除项的id
   */
  public void delete(Long id){
    Assert.isTrue(id != null,
      "删除数据必须指定数据删除条件（主键）");
    {{camelTableName}}Mapper.deleteById(id);
  }

}
